import React from 'react';
import util from 'util';
import {Table,Modal,Button } from 'antd';
import { ColsEdit} from 'kpl';
import sys_dict from "sys_dict";

// 服务管理
class Taskparam extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            tblSource: [],
            totalCount: 0,
            isLoading: false,
        };
        // 初始化查询参数
        this.searchParams={
            "page": 1,
            "rows": sys_dict.defaultPageSize.toString()
        };
    }
    // 渲染之后 qwe
    componentDidMount() {
         this.rights=util.checkRights(["m961"]);
        //this.rights=util.checkRights(["153"]);
        this.refresh(this.searchParams);
    }

    // 刷新123
    refresh(paramdic) {
        // 打开等待框
        this.setState({isLoading: true});
        util.httpRequest({
            type: "POST", url: 'task050501.json', params: paramdic,
            success: function (result) {
                if (result.ret_code == "0000") {
                    this.setState({
                        tblSource: result.rows,
                        totalCount: result.total
                    });
                }
            }.bind(this)
            ,complete: function () {
                this.setState({
                    isLoading: false
                })
            }.bind(this)
        });
    }

    // pagesize改变
    onShowSizeChange(current, pageSize) {
        console.log(current, pageSize);
        let params = this.searchParams;
        params.rows = pageSize;
        params.page = current;
        this.refresh(params);
    }
    // page改变
    onChange(page) {
        console.log(page);
        let params = this.searchParams;
        params.page = page;
        this.refresh(params);
    }

    // 打开编辑
    edit(record, index) {
        this.setState({
            operateData: record,
            modalVisible: true,
            operateType: 'EDIT'
        }, function () {
            var {setFieldsValue} = this.refs.form.getForm();
            setFieldsValue(record);
        });
    }

    // 初始化配置 1
    config() {
        const pagination = {
            total: this.state.totalCount,
            showSizeChanger: true,
            showQuickJumper: true,
            pageSizeOptions: sys_dict.pageSizeOptions,
            defaultPageSize: sys_dict.defaultPageSize,
            onShowSizeChange: this.onShowSizeChange.bind(this),
            onChange: this.onChange.bind(this)
        };
        // 返回列表配置
        return {
            tableConfig: {
                pagination: pagination,
                loading: this.state.isLoading,
                dataSource: this.state.tblSource,
                columns: [ {
                    title: '编码',
                    dataIndex: 'code',
                    key: 'code'
                },{
                    title: '名称',
                    dataIndex: 'name',
                    key: 'name'
                }, {
                    title: '参数值',
                    dataIndex: 'value',
                    key: 'value'
                },{
                    title: '备注',
                    dataIndex: 'remark',
                    key: 'remark'
                },{
                    title: '用户操作',
                    key: 'operation',
                    render: (text, record, index) => (
                        <span>
                            {this.rights[0]?<a onClick={this.edit.bind(this,record,index)}>修改</a>:null}
                        </span>
                    )
                }]
            }
        };
    }

    // 取消弹出框
    handleCancel() {
        this.setState({
            modalVisible: false
        });
    }
    // 保存
    onSave() {
        var form = this.refs.form.getForm();
        form.validateFields((errors, values)=> {
            if (!!errors) {
                return;
            }
            // 关闭弹出框
            this.setState({
                modalVisible: false
            });
            // 编辑
            values.id=this.state.operateData.id;
            this.update(values);
        });
    }

    // 更新
    update(values) {
        // 修改角色信息
        util.httpRequest({
            type: "POST", url: 'task050502.json', params: values,
            success: function (result) {
                if (result.ret_code == "0000") {
                    this.refresh(this.searchParams);
                }
            }.bind(this)
        });
    }

    // 渲染
    render() {
        let config = this.config();

        // 新增任务
        const createConfig = {
            operateTitle: "新增任务",
            operateType: this.state.operateType,
            operateData: this.state.operateData,
            operateInit: {rolename: "", roletype: "", descript: ""},
            operateColSm: 24,
            operateCol:{labelCol:4,wrapperCol:20},
            operateField: [
                [
                    {
                        propsname: "编码",
                        propsKey: "code",
                        propsType: "input",
                        key: "111",
                        editDisable:true,
                        rules: [{required: true, whitespace: true, min: 0, message: '请输入'}]
                    }
                ],
                [
                    {
                        propsname: "名称",
                        propsKey: "name",
                        propsType: "input",
                        key: "112",
                        rules: [{required: true, min: 0, message: '请输入'}]
                    }
                ],
                [
                    {
                        propsname: "参数值",
                        propsKey: "value",
                        propsType: "input",
                        key: "113",
                        rules: [{required: true, min: 0, message: '请输入'}]
                    }
                ],
                [
                    {
                        propsname: "备注",
                        propsKey: "remark",
                        propsType: "textarea",
                        key: "114",
                        optionData: [],
                        rules: [{required: true,whitespace: true, message: '请输入'}],
                        labelCol: 4,
                        wrapperCol: 20
                    }
                ]
            ]
        };

        return (
            <div>
                <Table {...config.tableConfig}></Table>
                <Modal visible={this.state.modalVisible} title={this.state.modalTitle}
                       closable={false} footer={[
                        <Button key="back" type="ghost" size="large" onClick={this.handleCancel.bind(this)}>取消</Button>,
                        <Button key="submit" type="primary" size="large" onClick={this.onSave.bind(this)}>保存</Button>
                    ]}>
                    <ColsEdit ref="form" key="create" config={createConfig}></ColsEdit>
                </Modal>
            </div>
        );
    }
}

export default Taskparam;